package com.gwclh.personnel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.*;
import com.gwclh.personnel.mapper.InternshipMapper;
import com.gwclh.personnel.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwclh.personnel.vo.internshipvo.InternshipEmpVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 实习表，说明，若要查询实习员工，通过查询状态为0与2的emp_id，若查询已转正员工，则查询状态为1 服务实现类
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@Service
public class InternshipServiceImpl extends ServiceImpl<InternshipMapper, Internship> implements IInternshipService {

    @Autowired
    private InternshipMapper internshipMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IStationService stationService;

    @Autowired
    private IUserService userService;

    @Override
    public IPage<Map<String, Object>> getInternship() {
        Page<Map<String, Object>> mapPage = new Page<>(1,5);
        IPage<Map<String, Object>> internship = internshipMapper.getInternship(mapPage, new QueryWrapper());
        return internship;
    }

    @Override
    public IPage<Map<String, Object>> getInternshipBySearch(Integer empId, String empName
            , String deptName, String staName, Integer internStatus
            , Date startDate, Date endDate) {
        Page<Map<String, Object>> mapPage = new Page<>(1,5);
        IPage<Map<String, Object>> internshipBySearch = internshipMapper.getInternshipBySearch(mapPage
                , new QueryWrapper(), empId, empName, deptName, staName, internStatus, startDate, endDate);
        return internshipBySearch;
    }

    @Override
    public IPage<Map<String, Object>> selectFormal() {
        Page<Map<String, Object>> mapPage = new Page<>(1,5);
        IPage<Map<String, Object>> mapIPage = internshipMapper.selectFormal(mapPage, new QueryWrapper());
        return mapIPage;
    }

    public IPage<Map<String, Object>> selectFormalBySearch(Integer empId, String empName
            , String deptName, Date startDate, Date endDate) {
        Page<Map<String, Object>> mapPage = new Page<>(1,5);
        IPage<Map<String, Object>> mapIPage = internshipMapper.selectFormalBySearch(mapPage, new QueryWrapper(), empId, empName, deptName, startDate, endDate);
        return mapIPage;
    }

    @Override
    public Integer operate(Integer id, Integer operate, String comment, Date operDate, String remark, Integer operId) {
        QueryWrapper<Internship> queryWrapper = Wrappers.query();
        queryWrapper.eq("emp_id",id);
        Internship internship = internshipMapper.selectOne(queryWrapper);
        switch (operate){
            case 1:
                internship.setInternStatus(1);
                break;
            case 2:
                internship.setInternStatus(2);
                break;
            case 3:
                internship.setInternStatus(3);
                break;
            default:
                break;
        }
        internship.setComment(comment);
        internship.setOperDate(operDate);
        internship.setRemark(remark);
        internship.setOperId(operId);
        int result = internshipMapper.insert(internship);
        return result;
    }


    /**
     * 统计当前月入职员工数
     * @return
     */
    @Override
    public Map<String, Object> statisticsInternshipMonth() {
        return internshipMapper.statisticsInternshipMonth();
    }

    /**
     * 查询试用期员工
     * @param pageDomain
     * @param internship
     * @return
     */
    @Override
    public ResultVo listInternship(PageDomain pageDomain, InternshipEmpVo internship) {
        Page<Employee> employeePage = new Page<>(pageDomain.getPageNum(),pageDomain.getPageSize());
        employeePage.addOrder(pageDomain.getOrderItemBy());

        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();

        employeeQueryWrapper.eq("is_internship",1)
                            .eq("status",1);

        if(internship.getId()!= null){
            employeeQueryWrapper.eq("id",internship.getId());
        }

        if(internship.getEmpName() != null){
            employeeQueryWrapper.like("emp_name",internship.getEmpName());
        }

        if(internship.getDeptId()!=null){
            employeeQueryWrapper.eq("dept_id",internship.getDeptId());
        }

        if(internship.getStaId() != null){
            employeeQueryWrapper.eq("sta_id",internship.getStaId());
        }

        employeeService.page(employeePage,employeeQueryWrapper);

        List<InternshipEmpVo> internshipEmpVoList = new ArrayList<>();
        employeePage.getRecords().forEach(item -> {

            QueryWrapper<Internship> internshipQueryWrapper = new QueryWrapper<>();
            internshipQueryWrapper.eq("emp_id",item.getId());
            if(internship.getStartDate() != null){
                internshipQueryWrapper.eq("start_date",internship.getStartDate());
            }
            if(internship.getEndDate()!= null){
                internshipQueryWrapper.eq("end_date",internship.getEndDate());
            }
            if(internship.getInternStatus() != null){
                internshipQueryWrapper.eq("intern_status",internship.getInternStatus());
            }

            Internship internship1 = this.getOne(internshipQueryWrapper);

            if(internship1!=null){
                InternshipEmpVo internshipEmpVo = new InternshipEmpVo();
                BeanUtils.copyProperties(item,internshipEmpVo);

                Department department = departmentService.getById(item.getDeptId());
                Station station = stationService.getById(item.getStaId());

                internshipEmpVo.setDeptName(department.getDeptName());

                internshipEmpVo.setStaName(station.getStaName());

                internshipEmpVo.setInternshipId(internship1.getId());
                internshipEmpVo.setStartDate(internship1.getStartDate());
                internshipEmpVo.setEndDate(internship1.getEndDate());
                internshipEmpVo.setInternStatus(internship1.getInternStatus());
                internshipEmpVoList.add(internshipEmpVo);
            }



        });

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("datas",internshipEmpVoList);
        resultMap.put("total",employeePage.getTotal());
        return ResultVo.success(resultMap);
    }

    @Override
    public ResultVo listTurnRightInternship(PageDomain pageDomain, InternshipEmpVo internshipEmpVo) {
        Page<Internship> internshipPage = new Page<>(pageDomain.getPageNum(),pageDomain.getPageSize());
        QueryWrapper<Internship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("intern_status",1);
        this.page(internshipPage,queryWrapper);
        List<InternshipEmpVo> internshipEmpVos = new ArrayList<>();
        internshipPage.getRecords().forEach(internship -> {

            QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();

            employeeQueryWrapper.eq("id",internship.getEmpId());

            if(internshipEmpVo.getId()!= null){
                employeeQueryWrapper.eq("id",internshipEmpVo.getId());
            }

            if(internshipEmpVo.getEmpName()!=null){
                employeeQueryWrapper.like("emp_name",internshipEmpVo.getEmpName());
            }

            if(internshipEmpVo.getDeptId() != null){
                employeeQueryWrapper.eq("dept_id",internshipEmpVo.getDeptId());
            }

            if(internshipEmpVo.getStaId() != null){
                employeeQueryWrapper.eq("sta_id",internshipEmpVo.getStaId());
            }

            Employee employee = employeeService.getOne(employeeQueryWrapper);
            if(employee!=null){
                InternshipEmpVo internshipEmpVo1 = new InternshipEmpVo();

                BeanUtils.copyProperties(employee,internshipEmpVo1);

                Department department = departmentService.getById(employee.getDeptId());
                internshipEmpVo1.setDeptName(department.getDeptName());

                Station station = stationService.getById(employee.getStaId());
                internshipEmpVo1.setStaName(station.getStaName());

                internshipEmpVo1.setInternshipId(internship.getId());
                internshipEmpVo1.setStartDate(internship.getStartDate());
                internshipEmpVo1.setEndDate(internship.getEndDate());
                internshipEmpVo1.setInternStatus(internship.getInternStatus());
                internshipEmpVo1.setRemark(internship.getRemark());
                internshipEmpVo1.setOperDate(internship.getOperDate());
                internshipEmpVo1.setComment(internship.getComment());

                User user = userService.getById(internship.getOperId());
                internshipEmpVo1.setOperName(user.getUsername());

                internshipEmpVos.add(internshipEmpVo1);
            }
        });
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("datas",internshipEmpVos);
        resultMap.put("total",internshipPage.getTotal());
        return ResultVo.success(resultMap);
    }
}
